package com.yuntianshi.service.impl;

import com.yuntianshi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SpringSecurityUserService implements UserDetailsService {
    @Autowired
    private UserService userService;

    //根据用户名查询用户信息
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        com.yuntianshi.pojo.User user = userService.findByUsername(username);
        if(user == null){
            //用户名不存在
            throw new RuntimeException("用户名或密码错误");
        }
        List<GrantedAuthority> list = new ArrayList<>();
        UserDetails userDetails = new User(username,user.getPassword(),list);
        return userDetails;
    }
}
